package com.zenithmind.library.pojo.domain;

import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.zenithmind.common.base.BaseEntity;
import com.zenithmind.library.enums.BookStatus;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;

import java.time.LocalDate;

/**
 * 图书副本实体
 * 
 * @author ZenithMind
 * @since 2024-06-14
 */
@EqualsAndHashCode(callSuper = true)
@Data
@AllArgsConstructor
@NoArgsConstructor
@Schema(name = "图书副本实体", description = "图书副本信息")
@TableName("lib_book_copy")
public class BookCopy extends BaseEntity {

    @NotNull(message = "图书ID不能为空")
    @Schema(description = "图书ID")
    private String bookId;

    @NotBlank(message = "条形码不能为空")
    @Size(max = 50, message = "条形码长度不能超过50个字符")
    @Schema(description = "条形码")
    private String barcode;

    @Schema(description = "RFID标签")
    @Size(max = 50, message = "RFID标签长度不能超过50个字符")
    private String rfidTag;

    @Schema(description = "图书状态")
    private Integer status;

    @Schema(description = "采购日期")
    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private LocalDate acquisitionDate;

    @Schema(description = "位置信息")
    @Size(max = 200, message = "位置信息长度不能超过200个字符")
    private String location;

    @Schema(description = "书架号")
    @Size(max = 50, message = "书架号长度不能超过50个字符")
    private String shelfNumber;

    @Schema(description = "层号")
    private Integer floorNumber;

    @Schema(description = "区域")
    @Size(max = 100, message = "区域长度不能超过100个字符")
    private String area;

    @Schema(description = "当前借阅者ID")
    private String currentBorrowerId;

    @Schema(description = "最后借阅日期")
    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private LocalDate lastBorrowDate;

    @Schema(description = "借阅次数")
    private Integer borrowCount;

    @Schema(description = "预约者ID")
    private String reservedByUserId;

    @Schema(description = "预约到期日期")
    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private LocalDate reservationExpireDate;

    @Schema(description = "图书状况描述")
    @Size(max = 500, message = "图书状况描述长度不能超过500个字符")
    private String condition;

    @Schema(description = "备注")
    @Size(max = 500, message = "备注长度不能超过500个字符")
    private String remarks;

    /**
     * 获取图书状态枚举
     */
    public BookStatus getBookStatus() {
        return BookStatus.getByCode(this.status);
    }

    /**
     * 设置图书状态枚举
     */
    public void setBookStatus(BookStatus bookStatus) {
        this.status = bookStatus != null ? bookStatus.getCode() : null;
    }

    /**
     * 判断是否可借阅
     */
    public boolean isAvailable() {
        BookStatus bookStatus = getBookStatus();
        return bookStatus != null && bookStatus.isAvailable();
    }

    /**
     * 判断是否已借出
     */
    public boolean isBorrowed() {
        BookStatus bookStatus = getBookStatus();
        return bookStatus == BookStatus.BORROWED;
    }

    /**
     * 判断是否已预约
     */
    public boolean isReserved() {
        BookStatus bookStatus = getBookStatus();
        return bookStatus == BookStatus.RESERVED;
    }

    /**
     * 判断是否需要维护
     */
    public boolean needsMaintenance() {
        BookStatus bookStatus = getBookStatus();
        return bookStatus == BookStatus.MAINTENANCE ||
               bookStatus == BookStatus.DAMAGED;
    }

    /**
     * 判断是否不可用（损坏、丢失、下架）
     */
    public boolean isUnavailable() {
        BookStatus bookStatus = getBookStatus();
        return bookStatus == BookStatus.DAMAGED ||
               bookStatus == BookStatus.LOST ||
               bookStatus == BookStatus.WITHDRAWN;
    }

    /**
     * 获取完整位置信息
     */
    public String getFullLocation() {
        StringBuilder location = new StringBuilder();

        if (area != null && !area.trim().isEmpty()) {
            location.append(area);
        }

        if (shelfNumber != null && !shelfNumber.trim().isEmpty()) {
            if (location.length() > 0) {
                location.append(" - ");
            }
            location.append("书架: ").append(shelfNumber);
        }

        if (floorNumber != null) {
            if (location.length() > 0) {
                location.append(" - ");
            }
            location.append("层: ").append(floorNumber);
        }

        if (this.location != null && !this.location.trim().isEmpty()) {
            if (location.length() > 0) {
                location.append(" - ");
            }
            location.append(this.location);
        }

        return location.toString();
    }

    /**
     * 检查是否可以借阅给指定用户
     */
    public boolean canBorrowTo(String userId) {
        if (!isAvailable()) {
            return false;
        }

        // 如果已被预约，检查是否为预约用户
        if (isReserved() && reservedByUserId != null) {
            return reservedByUserId.equals(userId);
        }

        return true;
    }


}
